//
//  WmcDevNetworkApiDetailVC.swift
//  YPWatermarkCamera
//
//  Created by 周诗松 on 2022/6/22.
//

import UIKit

// MARK: - 扩展WmcDevNetworkApiDetailVC + WmcVMProtocol

extension WmcDevNetworkApiDetailVC: WmcVMProtocol {
    typealias VMType = WmcDevNetworkApiDetailVM
}

// MARK: - [WmcDevNetworkApiDetailVC]

class WmcDevNetworkApiDetailVC: WmcBaseVC {
    // Lifecycle

    override func viewDidLoad() {
        super.viewDidLoad()
        title = viewModel.model.name
    }

    // Internal

    override func makeUI() {
        makeNavView(showShadow: false)

        navView.addSubview(requestBtn)
        addSubview(titleView_1)
        addSubview(tableView)
        addSubview(titleView_2)
        addSubview(respTextView)

        requestBtn.snp.makeConstraints { make in
            make.right.equalTo(-5)
            make.width.equalTo(60)
            make.height.equalTo(50)
            make.centerY.equalToSuperview().offset(10)
        }

        titleView_1.snp.makeConstraints { make in
            make.top.equalTo(navView.snp.bottom).offset(10)
            make.left.right.equalToSuperview()
            make.height.equalTo(25)
        }

        let height = (self.view.bounds.height - navView.bounds.height - 150) / 2.0
        tableView.snp.makeConstraints { make in
            make.top.equalTo(titleView_1.snp.bottom)
            make.left.right.equalToSuperview()
            make.height.equalTo(height)
        }

        titleView_2.snp.makeConstraints { make in
            make.top.equalTo(tableView.snp.bottom)
            make.left.right.equalToSuperview()
            make.height.equalTo(25)
        }

        respTextView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(titleView_2.snp.bottom)
        }
    }

    override func bindViewModel() {
        YPNetworkLogger.shared.logSubject.subscribe(onNext: { log in
            DispatchQueue.main.async {
                self.logText.append(contentsOf: log)
                self.logText.append(contentsOf: "\n")
                self.respTextView.text = self.logText
            }
        }).disposed(by: rx.disposeBag)
    }

    // Private

    private lazy var tableView: UITableView = {
        let view = UITableView(frame: CGRect.zero, style: .plain)
        view.backgroundColor = .wmc_white_FFFFFF
        view.delegate = self
        view.dataSource = self
        view.register(WmcDevNetworkParamsCell.self, forCellReuseIdentifier: "WmcDevNetworkParamsCell")
        view.separatorStyle = .none
        return view
    }()

    private lazy var requestBtn: UIButton = {
        let btn = UIButton(type: .system)
        btn.setTitle("提交", for: .normal)
        btn.addTarget(self, action: #selector(onSubmit), for: .touchUpInside)
        return btn
    }()

    private lazy var respTextView: UITextView = {
        let view = UITextView()
        view.isEditable = false
        view.backgroundColor = .wmc_grayH17_EBF2FF
        return view
    }()

    private lazy var titleView_1: UIView = {
        let view: UIView = .init()
        view.backgroundColor = .wmc_grayH22_FFF5E7

        let label = UILabel()
        label.text = "请求参数"
        label.font = UIFont.systemFont(ofSize: 14, weight: .medium)
        view.addSubview(label)

        label.snp.makeConstraints { make in
            make.left.equalTo(12)
            make.right.top.bottom.equalToSuperview()
        }

        return view
    }()

    private lazy var titleView_2: UIView = {
        let view: UIView = .init()
        view.backgroundColor = .wmc_grayH22_FFF5E7

        let label = UILabel()
        label.text = "响应结果"
        label.font = UIFont.systemFont(ofSize: 14, weight: .medium)
        view.addSubview(label)

        label.snp.makeConstraints { make in
            make.left.equalTo(12)
            make.right.top.bottom.equalToSuperview()
        }

        return view
    }()

    private var logText: String = ""

    @objc
    private func onSubmit() {
        respTextView.text = nil
        logText = ""
        viewModel.onRequest()
    }
}

// MARK: - UITableViewDelegate, UITableViewDataSource

extension WmcDevNetworkApiDetailVC: UITableViewDelegate, UITableViewDataSource {
    func numberOfSections(in _: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return viewModel.dataSource.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell: WmcDevNetworkParamsCell = tableView.dequeueReusableCell(withIdentifier: "WmcDevNetworkParamsCell", for: indexPath) as! WmcDevNetworkParamsCell
        let model = viewModel.dataSource[indexPath.row]
        cell.bindModel(model)
        cell.inputClosure = { text in
            model.value = text
            tableView.reloadData()
        }
        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {}

    func tableView(_: UITableView, heightForRowAt _: IndexPath) -> CGFloat {
        return 80
    }
}
